﻿
#include <algorithm>
#include <gtest/gtest.h>
#include <iostream>

#include <xy/solver/nonlinear.h>

using namespace xy;
using namespace xy::math;
using namespace xy::solver;

// 测试 NewtonSolver
TEST(TestEquation, solving)
{
    func11 f1 = [](double x) {
        double y = x + sin(x) - 0.5 * x * x;
        return y;
    };

    nonlinear solver;
    double x0 = solver.solve(f1, 1);
    EXPECT_LT(f1(x0), 1e-6);
    func11 f2 = [](double x) {
        double y = tan(x) + cos(x) - 0.5 * x * x;
        return y;
    };

    x0 = solver.solve(f2, 0);
    EXPECT_LT(f2(x0), 1e-6);

    funcxx f3 = [=](vecxd x) -> vecxd { return {f1(x[0]), f2(x[1])}; };
    vecxd X0{10, 20};
    X0 = solver.solve(f3, X0);
    EXPECT_LT((f3(X0) - vecxd(2)).norm(), 1e-6);

    funcxx f4 = [=](vecxd x) -> vecxd { return {f1(f2(x[0])), f2(f1(x[1]))}; };
    vecxd X1{30, 40};
    X1 = solver.solve(f4, X1);
    EXPECT_LT((f4(X1) - vecxd(2)).norm(), 1e-6);
}